$.eud.mdm = $.eud.mdm || {};
var cityJson;

$.eud.mdm.mdmCustomer = {
	//基类名字
	pageName:"BaseSimplePage",
	init:function(){
		var me = this;
        var page = $("#mdmCustomer")[me.pageName]();

        $.getJSON("static/js/mdm/city.json",
            function(obj) {
                cityJson = obj;

                var province = [];
                var city = [];
                var county = [];
                $.each(obj, function(i, val) {
                    if(val.item_code.substr(2, 4) == '0000'){
                        province.push(val);
                    }
                    if (val.item_code.substr(4, 2) == '00' || val.item_code == '520601') {
                        city.push(val);
                    }
                    if (val.item_code.substr(2, 4) != '0000' && val.item_code.substr(4, 2) != '00' && val.item_code != '520601') {
                        county.push(val);
                    }
                });
                $.eud.mdm.mdmCustomer.data.provinceData=province;
                $.eud.mdm.mdmCustomer.data.cityData = city;
                $.eud.mdm.mdmCustomer.data.countyData = county;
            });

        $.eud.loader.get(BasePath+'/api/basDictApi/getDtlByDictCode.json?dictCode=customer_type', function(data){
            $.eud.mdm.mdmCustomer.data.customerTypeData = data;
        });
	},
	/**
     * 初始化页面事件
     */
    initEvents: function(){
    	var me =this;
    },
    //重载方法
    override:{
        onGridEndEdit: function (rowIndex, rowData, changes) {
            var me = this;
            $.eud.mdm.mdmCustomer.callParent(me);
            if (changes["province"]){
                var province = rowData["province"];
                var city = rowData["city"];
                var county = rowData["county"];
                var street = rowData["street"];
                if (street != undefined) {
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{street:""}})
                }
                if (city != undefined){
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{city:""}})
                }
                if (county != undefined) {
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{county:""}})
                }
                var cityData = [];
                $.each(cityJson, function(i, val) {
                    if(val.item_code.substr(0, 2) == province.substr(0, 2) && val.item_code.substr(2, 4) != '0000' && val.item_code.substr(4, 2) == '00'){
                        cityData.push(val);
                    }
                });
                $.eud.mdm.mdmCustomer.data.cityData=cityData;
            }
            if (changes["city"]){
                var province = rowData["province"];
                var city = rowData["city"];
                var county = rowData["county"];
                var street = rowData["street"];
                if (street != undefined) {
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{street:""}})
                }
                if (county != undefined) {
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{county:""}})
                }
                var countyData = [];
                $.each(cityJson, function(i, val) {
                    if(city=='110100' || city=="120100" || city=="310100" || city=="500100"){
                        if(val.item_code.substr(0, 3) == city.substr(0, 3) && val.item_code.substr(4, 2) != '00'){
                            countyData.push(val);
                        }
                    }else{
                        if(val.item_code.substr(0, 4) == city.substr(0, 4) && val.item_code.substr(4, 2) != '00'){
                            countyData.push(val);
                        }
                    }
                });
                $.eud.mdm.mdmCustomer.data.countyData=countyData;
            }
            if (changes["county"]){
                var county = rowData["county"];
                var street = rowData["street"];
                if (street != undefined) {
                    $(me.scope.grid1).datagrid("updateRow",{index:rowIndex,row:{street:""}})
                }
                var streetUrl = "http://restapi.amap.com/v3/config/district?subdistrict=1&level=district&showbiz=false&extensions=all&key=73aab7b5205ed894ed787d60ef7fddc8&s=rsv3&output=json&keywords=" + county + "&platform=JS&logversion=2.0&sdkversion=1.3";
                $.getJSON(streetUrl, function (obj) {
                    if (obj.status == "1") {
                        var streetData = obj.districts[0].districts;
                        $.eud.mdm.mdmCustomer.data.streetData = streetData;
                    }
                });
            }
        },
        onGridBeforeCellEdit:function(row,index,field,val){
            if (field == "city" && row.province){
                var cityData = [];
                $.each(cityJson, function(i, val) {
                    if(val.item_code.substr(0, 2) == row.province.substr(0, 2) && val.item_code.substr(2, 4) != '0000' && val.item_code.substr(4, 2) == '00'){
                        cityData.push(val);
                    }
                });
                $.eud.mdm.mdmCustomer.data.cityData=cityData;
            }else if(field == "county" && row.city){
                var countyData = [];
                $.each(cityJson, function(i, val) {
                    if(row.city=='110100' || row.city=="120100" || row.city=="310100" || row.city=="500100"){
                        if(val.item_code.substr(0, 3) == row.city.substr(0, 3) && val.item_code.substr(4, 2) != '00'){
                            countyData.push(val);
                        }
                    }else{
                        if(val.item_code.substr(0, 4) == row.city.substr(0, 4) && val.item_code.substr(4, 2) != '00'){
                            countyData.push(val);
                        }
                    }
                });
                $.eud.mdm.mdmCustomer.data.countyData=countyData;
            }else if(field == "street" && row.county){
                var streetUrl = "http://restapi.amap.com/v3/config/district?subdistrict=1&level=district&showbiz=false&extensions=all&key=73aab7b5205ed894ed787d60ef7fddc8&s=rsv3&output=json&keywords=" + row.county + "&platform=JS&logversion=2.0&sdkversion=1.3";
                $.getJSON(streetUrl, function (obj) {
                    if (obj.status == "1") {
                        var streetData = obj.districts[0].districts;
                        $.eud.mdm.mdmCustomer.data.streetData = streetData;
                    }
                });
            }
        }
    },
    data:{
        statusData:[{"itemCode":"1","itemName":"启用"},{"itemCode":"0","itemName":"禁用"}],//启用状态
        ratepayLevelData:[{"itemCode":"1","itemName":"小规模纳税人"},{"itemCode":"0","itemName":"一般纳税人"}],
		provinceData:[],
        cityData:[],
        countyData:[],
        streetData:[],
        customerTypeData:[]
    },
    formatter:{
        status:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "itemCode","itemName",  $.eud.mdm.mdmCustomer.data.statusData);
        },
		province:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "item_code","item_name",  $.eud.mdm.mdmCustomer.data.provinceData);
        },
        city:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "item_code","item_name",  $.eud.mdm.mdmCustomer.data.cityData);
        },
        county:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "item_code","item_name",  $.eud.mdm.mdmCustomer.data.countyData);
        },
        street: function (val, item) {
            return $.eud.utils.comboboxFormatter(val, "name", "name", $.eud.mdm.mdmCustomer.data.streetData);
        },
        ratepayLevel:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "itemCode","itemName",  $.eud.mdm.mdmCustomer.data.ratepayLevelData);
        },
        customerType:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "itemCode","itemName",  $.eud.mdm.mdmCustomer.data.customerTypeData);
        }
    }
};